package category.model.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import category.domain.Product;
import category.util.DatabaseManager;
//싱글톤
public class CategoryDAO {
	private static CategoryDAO instance = new CategoryDAO();
	private SqlSessionFactory factory;
	private CategoryDAO(){
		factory = DatabaseManager.getInstance().getSqlSessionFactory();
	}
	public static CategoryDAO getInstance(){
		return instance;
	}
	/**
	 * 대분류 상품 카테고리 조회
	 * @return
	 * @throws SQLException
	 */
	public List<HashMap<String, String>> selectAllFirstCategory() throws SQLException{
		SqlSession session = factory.openSession();
		try{
			return session.selectList(makeSqlId("selectAllFirstCategory"));
		}finally{
			session.close();
		}
	}
	/**
	 * 중분류 상품 카테고리 조회
	 * @param firstCategory 대분류 ID
	 * @return
	 * @throws SQLException
	 */
	public List<HashMap<String, String>> selectSecondCategoryByFId(String firstCategoryId) throws SQLException{
		SqlSession session = factory.openSession();
		try{
			return session.selectList(makeSqlId("selectSecondCategoryByFId"), firstCategoryId);
		}finally{
			session.close();
		}
	}
	/**
	 * 소분류 상품 카테고리 조회
	 * @param secondCategory 중분류  ID
	 * @return
	 * @throws SQLException
	 */
	public List<HashMap<String, String>> selectThirdCategoryBySId(String secondCategoryId) throws SQLException{
		SqlSession session = factory.openSession();
		try{
			return session.selectList(makeSqlId("selectThirdCategoryBySId"), secondCategoryId);
		}finally{
			session.close();
		}
	}
	/**
	 * 소분류를 받아 그 소분류에 속한 상품 리스트를 조회
	 * @param categoryId : 조회할 소분류 ID
	 * @return
	 * @throws SQLException
	 */
	public List<Product> selectProductInfoByTId(String thirdCategoryId) throws SQLException{
		SqlSession session = factory.openSession();
		try{
			return session.selectList(makeSqlId("selectProductInfoByTId"), thirdCategoryId);
		}finally{
			session.close();
		}
	}
	
	private String makeSqlId(String id){
		return "category."+id;
	}
}
